const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proId:0,
    box:1,
    ProvinceAndCity:[],
    btncurr:false,
    buxian:0,
    btnName:'完成',
    cirylist:[],
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    var ProAndCity = wx.getStorageSync('ProvinceAndCityKey');
    for (var i = 0; i < ProAndCity[options.proId].list.length; i++) {
      ProAndCity[options.proId].list[i].sel = 0;
    }
    this.setData({
      proId:options.proId,
      box:options.box,
      ProvinceAndCity:ProAndCity[options.proId]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  selectPr:function (e) {
    var that = this;
    var index = e.currentTarget.dataset.idx;    
    var city = this.data.ProvinceAndCity;
    var cirylist = this.data.cirylist;
    if (index == 0) {
      if (cirylist.length > 0) {
        wx.showModal({
          title: '尊敬的用户',
          content: '是否要选择不限,选择不限后其他选项将被清空',
          success (res) {
            if (res.confirm) {
              for (var i = 1; i < city.list.length; i++) {
                city.list[i].sel = 0;
              }
              that.setData({
                buxian:1,
                ProvinceAndCity:city,
                ProList:[]                
              })
            } 
          }
        })
      }else{

      }
    }else{
      this.setData({
        buxian:0
      })
      city.list[0].sel = 0;
      if (cirylist.indexOf(index) > -1) {
        cirylist.splice(cirylist.indexOf(index),1);
      }else{
        cirylist.push(index);      
      }      
    } 
    if (city.list[index].sel == 1) {
      city.list[index].sel = 0;
    }else{
      city.list[index].sel = 1;
    } 
    if (cirylist.length > 0) {
      this.setData({
        btncurr:true
      })
    }else{
      this.setData({
        btncurr:false
      })      
    }    
    this.setData({
      ProvinceAndCity:city
    })         
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  queding:function () {    
    var provcity = this.data.ProvinceAndCity;
    var prov = provcity.code;
    var cirylist = this.data.cirylist;
    var provname = provcity.name;
    var city = '';
    var cityname = '';
    if (this.data.buxian == 1) {
      cityname = '不限';
      city = '0';
    }else{
      if (cirylist.length > 0) {
        for (var i = 0; i < cirylist.length; i++) {
          city = city + provcity.list[cirylist[i]].code + ',';
          cityname = cityname + provcity.list[cirylist[i]].name + ',';
        }        
      }else{
        cityname = '不限';
        city = '0';        
      }
    }
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 3];
    cityname = (cityname.substring(cityname.length-1)==',')?cityname.substring(0,cityname.length-1):cityname;
    city = (city.substring(city.length-1)==',')?city.substring(0,city.length-1):city;
    prevPage.setData({
      prov:prov,
      provname:provname,
      cityname:cityname,
      city:city,
      provnamecname:provname+','+cityname
    })
    wx.navigateBack({
       delta: 2,
    })
  }
})